package com.ktg.handler;


import com.ktg.ktgcommon.result.R;
import com.ktg.ktgcommon.utils.JSONUtils;
import com.ktg.ktgcommon.utils.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        R result= R.FAIL(exception.getMessage());
        String resultJSON= JSONUtils.toJSON(result);
        ResponseUtils.write(response,resultJSON);
    }
}
